﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Forms;
using MessageBox = System.Windows.Forms.MessageBox;
using PixelFormat = System.Drawing.Imaging.PixelFormat;

namespace clipSa
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            textBoxPath.Text = MySettings.Sets.defCatalog;
            checkBoxAutoSave.IsChecked = MySettings.Sets.isAutoSave;
            comboBoxFormat.SelectedIndex = MySettings.Sets.defExt - 1;
            textBoxPrefix.Text = MySettings.Sets.prefixFileName;
            textBoxSufix.Text = MySettings.Sets.sufixFileName;
            textMinNN.Text = MySettings.Sets.minNN.ToString();
            textMaxNN.Text = MySettings.Sets.maxNN.ToString();
            comboBox1.DataContext = Enum.GetValues(typeof (System.Drawing.Imaging.PixelFormat));
            comboBox1.SelectedValue = 3;
            comboBox1.SelectedItem =MySettings.Sets.depth;
                
            if ( (bool) checkBoxAutoSave.IsChecked)
            {
                textBoxPrefix.IsEnabled = textBoxSufix.IsEnabled = textMinNN.IsEnabled =
                textMaxNN.IsEnabled = true;
                label3.IsEnabled = label4.IsEnabled = label6.IsEnabled = label7.IsEnabled = true;
            }
            else
            {
                textBoxPrefix.IsEnabled = textBoxSufix.IsEnabled = textMinNN.IsEnabled =
                textMaxNN.IsEnabled = false;
                label3.IsEnabled = label4.IsEnabled = label6.IsEnabled = label7.IsEnabled = false;
            }
        }

        private void ButtonCancel(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(textMinNN.Text) >= Convert.ToInt32(textMaxNN.Text))
                MessageBox.Show(Properties.Resources.ErrorMinMax);
            else
            {
                MySettings.Sets.defCatalog = textBoxPath.Text;
                if (checkBoxAutoSave.IsChecked != null)
                    if (checkBoxAutoSave != null) MySettings.Sets.isAutoSave = (bool) checkBoxAutoSave.IsChecked;
                MySettings.Sets.defExt = comboBoxFormat.SelectedIndex + 1;
                MySettings.Sets.depth= (PixelFormat) comboBox1.SelectedItem;
                MySettings.Sets.prefixFileName = textBoxPrefix.Text;
                MySettings.Sets.sufixFileName = textBoxSufix.Text;
                MySettings.Sets.minNN = Convert.ToInt32(textMinNN.Text);
                MySettings.Sets.maxNN = Convert.ToInt32(textMaxNN.Text);
                MySettings.Sets.Save();
                Close();
            }
        }

        private void CheckBoxAutoSaveChecked(object sender, RoutedEventArgs e)
        {
            textBoxPrefix.IsEnabled = textBoxSufix.IsEnabled = textMinNN.IsEnabled =
            textMaxNN.IsEnabled = true;
            label3.IsEnabled = label4.IsEnabled = label6.IsEnabled = label7.IsEnabled = true;
        }

        private void CheckBoxAutoSaveUnChecked(object sender, RoutedEventArgs e)
        {
            textBoxPrefix.IsEnabled = textBoxSufix.IsEnabled= textMinNN.IsEnabled=
            textMaxNN.IsEnabled = false;
            label3.IsEnabled = label4.IsEnabled = label6.IsEnabled = label7.IsEnabled = false;
        }

        private void buttonChoiceDir_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fBD = new FolderBrowserDialog {ShowNewFolderButton=true,
                                                               SelectedPath=MySettings.Sets.defCatalog
            };
            DialogResult result=fBD.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK) textBoxPath.Text = fBD.SelectedPath;
            
        }

        private void MinNNKeyDown(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !IsTextAllowed(e.Text);
        }


        // Use the PreviewTextInputHandler to respond to key presses
        private void MaxNNKeyDown(Object sender, TextCompositionEventArgs e)
        {
            e.Handled = !IsTextAllowed(e.Text);
        }

        // Use the DataObject.Pasting Handler 
        private void PastingHandler(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(String)))
            {
                String text = (String)e.DataObject.GetData(typeof(String));
                if (!IsTextAllowed(text)) e.CancelCommand();
            }
            else e.CancelCommand();
        }
        private Boolean IsTextAllowed(String text)
        {
            return Array.TrueForAll<Char>(text.ToCharArray(),
                delegate(Char c) { return Char.IsDigit(c) || Char.IsControl(c); });
        }
    }
}
